﻿using System.Collections.Generic;
using System.Text;
using EmbeddedSharp.Scripting.Function;

namespace Zove.Runtime.Core {
    public abstract class ZoomObject {

        public ZoomClass Parent { get; set; }

        public string Name { get; set; }

        public string Summary { get; set; }
    }

    public class ZoomFunction : ZoomObject {
        public string Name { get; set; }

        public string[] Parameters { get; set; }

        public string Body { get; set; }

        public string Description { get; set; }

        public int LineNumber { get; set; }

        public int Column { get; set; }
    }

    /// <summary>
    /// The variable
    /// </summary>
    public class ZoomVariable : ZoomObject {
        public string Type { get; set; }

        public string Unit { get; set; }

        public override string ToString() {
            return Type + " " + Name;
        }
    }

    public class ZoomConstant : ZoomObject {
        public string Value { get; set; }
    }

    public class ZoomElement {
        public string Name { get; set; }

        public string Expression { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }

    public class ZoomElementCollection : List<ZoomElement> {
        public ZoomElement this[string elementName] {
            get {
                foreach (ZoomElement element in this) {
                    if (element.Name == elementName) {
                        return element;
                    }
                }

                return null;
            }
        }
    }

    public class ZoomEquation : ZoomObject {
        public ZoomEquation() {
            Elements = new ZoomElementCollection();
        }

        public int LineNumber { get; set; }

        /// <summary>
        /// the equation
        /// </summary>
        public string Equation { get; set; }

        /// <summary>
        /// Left hand side expression
        /// </summary>
        public string Left { get; set; }

        /// <summary>
        /// Right hand side expression
        /// </summary>
        public string Right { get; set; }

        /// <summary>
        /// Elements in the equation
        /// </summary>
        public ZoomElementCollection Elements { get; set; }

        public override string ToString()
        {
            return Equation;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class ZoomConstantCollection : List<ZoomConstant> {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ZoomConstant GetConstant(string name) {
            foreach (ZoomConstant c in this) {
                if (c.Name == name) {
                    return c;
                }
            }

            return null;
        }
    }

    public class ZoomVariableCollection : List<ZoomVariable>
    {
        public ZoomVariable this[string variableName]
        {
            get
            {
                foreach(ZoomVariable variable in this)
                {
                    if (variable.Name == variableName)
                    {
                        return variable;
                    }
                }

                return null;
            }
        }
    }

    public class ZoomEquationCollection : List<ZoomEquation> {
        public ZoomEquation this[string variableName] {
            get {
                foreach (ZoomEquation equation in this) {
                    if (equation.Name == variableName) {
                        return equation;
                    }
                }

                return null;
            }
        }
    }

    public class ZoomClass : ZoomObject {
        public ZoomClass() {
            BaseClasses = new List<string>();
            Variables = new ZoomVariableCollection();
            Equations = new ZoomEquationCollection();
            Functions = new Dictionary<string, UserDefinedFunctionNode>();
            Constants = new ZoomConstantCollection();
        }

        public ZoomClass(string name)
            : this() {
            Name = name;
        }

        public string Url { get; set; }

        public List<string> BaseClasses { get; set; }

        public ZoomVariableCollection Variables { get; set; }

        public ZoomEquationCollection Equations { get; set; }

        public Dictionary<string, UserDefinedFunctionNode> Functions { get; set; }

        public ZoomConstantCollection Constants { get; set; }

        public void AddUserDefinedFunction(string name, UserDefinedFunctionNode fn) {
            Functions.Add(name, fn);
        }

        /// <summary>
        /// Check to see if there is variable
        /// </summary>
        /// <param name="variableName"></param>
        /// <returns></returns>
        public bool HasVariable(string variableName) {
            foreach (ZoomVariable variable in Variables) {
                if (variable.Name == variableName) {
                    return true;
                }
            }

            return false;
        }


        public override string ToString() {
            var b = new StringBuilder();

            b.AppendLine("class " + Name);
            if (BaseClasses.Count > 0) {
                b.Append(" extends ");
                for (int i = 0; i < BaseClasses.Count; i++) {
                    b.Append(BaseClasses[i]);
                    if (i + 1 < BaseClasses.Count) {
                        b.Append(",");
                    }
                }
                b.AppendLine();
            }
            b.AppendLine("{");
            foreach (ZoomVariable var in Variables) {
                b.AppendFormat("\t{0} {1};\n", var.Type, var.Name);
            }

            b.AppendLine();
            foreach (ZoomEquation zeq in Equations) {
                b.AppendLine("\t" + zeq.Equation + ";");
            }
            b.AppendLine("}");

            return b.ToString();
        }
    }
}